package com.pengliu.controller;

import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.pengliu.service.HttpService;
import com.pengliu.util.Maps;

@RestController
public class OssSignThirdController {
	
	/*请填写您的AccessKeyId*/
	private String accessId = "e440f071bb1744b1";
	
	/*请填写您的AccessKeySecret*/
	private String accessKey = "f2416cb0fc864e988e5056ca86770150";
	
	/*host的格式为 bucketname.endpoint*/
	@Value("${host}")
	private String host;
	
	@Autowired
	private HttpServletResponse response;
	
	@Autowired
	private HttpService httpService;
	
	@RequestMapping("/oss/getOssSign")
	private Map<String,Object> getOssSign(
		@RequestParam(name="bucket",defaultValue="/bucket_test") String bucket,
		@RequestParam(name="dir",defaultValue="/test") String dir		
	) throws Exception {
		if(!bucket.substring(0,1).equals("/")) {
    		bucket = "/".concat(bucket);
    	}
    	if(!dir.substring(0,1).equals("/")) {
    		dir = "/".concat(dir);
    	}
    	
    	Map<String,String> data = Maps.newConcurrentHashMap();
    	data.put("accessId",accessId);
    	data.put("accessKey",accessKey);
    	data.put("bucket",bucket);
    	data.put("dir",dir);
    	String reslutJson = httpService.requestGet(host+"/oss/authOss", data);
    	Map<String,Object> resultMap = JSON.parseObject(reslutJson, Map.class);
    	
    	response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET, POST");
    	
		return resultMap;
	}
}
